<?php

include_once('staff.php');

class subscriber_management extends staff
{
	function __construct()
    {
        // Call the Model constructor
        parent::__construct();
		
		// Load the form helper
		$this->load->helper('form');
		
		// Load the form validation library
		$this->load->library('form_validation');
		
		// Load the required models
		$this->load->model('subscriber_model');
		$this->load->model('subscriber_paid_model');
		$this->load->model('loan_model');
		$this->load->model('volume_model');
    }
	
	public function searchSubscribers($search_by='last_name', $term='ALL', $offset = 0)
	{
		// If the user has done a new search (i.e. submit button pressed)
		if ($this->input->post())
		{
			// Get post results and filter them to rid unacceptable characters
			$search_by = $this->input->post('search_by');
			$term = preg_replace('/[^a-zA-Z0-9]/', ' ', $this->input->post('term'));
	
			// If the form has been clicked, redirect
			redirect('staff/subscriber_management/searchSubscribers/' . $search_by . '/' . $term );
		}
	
		// Set this page as previous subscriber search
		$this->session->set_userdata('prev_subscriber_search', preg_replace('[%20]', ' ', current_url()));
	
		// Determine total number of matching results
		$this->data['result_count'] = $this->subscriber_model->getSearchCount($search_by, $term);
		
		// Get 20 results
		$this->data['subscribers'] = $this->subscriber_model->getSearchResults($search_by, $term, $offset, 20);
		
		// Setup pagination
		$this->load->library('pagination');
		$config['base_url'] = site_url('staff/subscriber_management/searchSubscribers/' . $search_by . '/' . $term);
		$config['total_rows'] = $this->data['result_count'];
		$config['per_page'] = 20;
		$config['num_links'] = 7;
		$config['uri_segment'] = 6;
		$this->pagination->initialize($config);
		
		// Load the "view_volume" view
		$this->data['main_content'] = $this->load->view('staff/subscriber_management/search_subscribers', $this->data, true);
		
		// Load $this->data['main_content'] in the main template
		$this->load->view($this->template, $this->data);
	}
	
	public function viewSubscriber($subscriber_id)
	{
		// Return subscriber information (Hello)
		$this->data['subscriber'] = $this->subscriber_model->getSubscriberByID($subscriber_id)->row();
	
		// Return subscriber-paid information for specified subscriber
		$this->data['subscriber_paid'] = $this->subscriber_paid_model->getSubscriberPaidByID( $subscriber_id );
		
		// Return loan information for specified subscriber
		$this->data['volumes'] = $this->loan_model->getVolumesLoanedBySubscriber( $subscriber_id );
		
		// Load the "view_volume" view
		$this->data['main_content'] = $this->load->view('staff/subscriber_management/view_subscriber', $this->data, true);
		
		// Load $this->data['main_content'] in the main template
		$this->load->view($this->template, $this->data);
	}
	
	public function deleteSubscriber($subscriber_id)
	{
		// Delete subscriber
		$this->subscriber_model->deleteSubscriberByID( $subscriber_id );
	
		redirect( $this->session->userdata('prev_subscriber_search') );
	}
	
	public function addSubscriber()
	{
		// Automatically place Subscriber ID in subscriber_id field
		$this->data['subscriber_id'] = $this->subscriber_model->getLastSubscriberID() + 1;
	
		// If 'POST' data exists (i.e. the submit button was clicked)
		if ( $this->input->post() )
		{	
			// If form validation succeeds (All fields correct)
			if ($this->form_validation->run('staff/subscriber_management'))
			{
				// Get the data to insert
				$data = $this->input->post();
			
				// Update volumes where access_id = $access_id with the values retrieved from the form
				$this->subscriber_model->addSubscriber( $data );
			
				redirect('staff/subscriber_management/viewSubscriber/' . $this->input->post('subscriber_id') );
			}
		}
		
		// Load the "add_subscriber" view
		$this->data['main_content'] = $this->load->view('staff/subscriber_management/add_subscriber', $this->data, true);
		
		// Load $this->data['main_content'] in the main template
		$this->load->view($this->template, $this->data);
	}
	
	public function modifySubscriber($subscriber_id)
	{
		// If 'POST' data exists (i.e. the submit button was clicked)
		if ( $this->input->post() )
		{
			// If form validation succeeds (All fields correct)
			if ($this->form_validation->run('staff/subscriber_management'))
			{
				// Get form data
				$form_data = $this->input->post();
				
				// Update subject
				$this->subscriber_model->updateSubscriber( $subscriber_id, $form_data );
			
				redirect('staff/subscriber_management/viewSubscriber/' . $form_data['subscriber_id'] );
			}
		}
	
		// Return subscriber information
		$this->data['subscriber'] = $this->subscriber_model->getSubscriberByID($subscriber_id)->row();
		
		// Load the "modify_volume" view
		$this->data['main_content'] = $this->load->view('staff/subscriber_management/modify_subscriber', $this->data, true);
		
		// Load $this->data['main_content'] in the main template
		$this->load->view($this->template, $this->data);
	}
	
	public function addPayment($subscriber_id = 0)
	{
		// If 'POST' data exists (i.e. the submit button was clicked)
		if ( $this->input->post() )
		{	
			// If form validation succeeds (All fields correct)
			if ($this->form_validation->run('staff/subscriber_management/addPayment'))
			{
				// Get the data to insert
				$payment_data = $this->input->post();
			
				// Add payment to tbl_subscriber_paid and set that subscriber to active
				$this->subscriber_paid_model->addPayment( $payment_data );
				$this->subscriber_model->updateSubscriber( $payment_data['subscriber_id'], array('subscriber_status'=>'A'));
			
				redirect('staff/subscriber_management/viewSubscriber/' . $this->input->post('subscriber_id') );
			}
		}
		
		// If we don't have a subscriber_id supplied, present a page to select one
		if (!$subscriber_id)
		{
			// Load the "add_payment" view
			$this->data['main_content'] = $this->load->view('staff/subscriber_management/add_payment_select_subscriber', $this->data, true);
		
			// Load $this->data['main_content'] in the main template
			$this->load->view($this->template, $this->data);
		}
		
		// Otherwise, we have a subscriber_id, so load the addPayment view
		else
		{               
			// Return subscriber information
			$this->data['subscriber'] = $this->subscriber_model->getSubscriberByID($subscriber_id)->row();
			
			// Get latest payment
			$this->data['subscriber_paid'] = $this->subscriber_paid_model->getSubscriberPaidByID($subscriber_id)->row();

			// Load the "add_subscriber" view
			$this->data['main_content'] = $this->load->view('staff/subscriber_management/add_payment', $this->data, true);

			// Load $this->data['main_content'] in the main template
			$this->load->view($this->template, $this->data);
		}
	}
}

?>